package uk.org.landeg.euler.problems;

import java.util.Set;
import java.util.TreeSet;

import uk.org.landeg.euler.problems.framework.AbstractProblem;
import uk.org.landeg.euler.problems.framework.ProblemLauncher;
import uk.org.landeg.euler.problems.lib.NumbersLib;
import uk.org.landeg.euler.problems.lib.PrimeLib;

public class Problem035 extends AbstractProblem{
	public static void main(String[] args) {
		ProblemLauncher launcher = new ProblemLauncher(new Problem035());
		launcher.solve();		
	}

	@Override
	public Object solve() {
		notes.add("If a prime contains 0,2 or 5 it will never be circular");
		notes.add("Generating primes < 1000000");
		Set<Integer> primes = new TreeSet<Integer>();
		Set<Integer> circulars = new TreeSet<Integer>();
		primes.addAll(PrimeLib.seqPrimes((int)1000000));
		int rotated;
		boolean circular;
		for (int trial : primes) {
			circular = true;
			rotated = trial;
			do {
				rotated = NumbersLib.rotateDigits(rotated);
				if (!primes.contains(rotated)) {
					circular = false;
					break;
				}
			} while (rotated != trial);
			if (circular) {
				circulars.add(trial);
			}
		}
		return circulars.size();
	}

	@Override
	public String getDescription() {
		// TODO Auto-generated method stub
		return "How many circular primes are there below one million?";
	}

	@Override
	public Integer getProblemNumber() {
		// TODO Auto-generated method stub
		return 35;
	}
}
